<?php

namespace App\Http\Controllers\Alipay;

use App\Models\Areas;
use App\Models\Category;
use App\Models\Goods;
use App\Models\ShopBanner;
use App\Models\ShopCategory;
use App\Models\ShopGoods;
use App\Models\ShopUser;
use App\Models\ShopUserLog;
use App\Services\CommonService;
use App\Services\GoodsService;
use App\Services\OtherCallbacksService;
use Illuminate\Http\Request;

class ShopController extends BaseVerifyController
{
    /**
     * 获取店铺轮播图
     */
    public function getShopBanner(Request $request){
        $member_id = $request->input('member_id');
        if($member_id){
            $data = ShopBanner::GetShopBannerByMemberid($member_id);
            if(!empty($data)){
                foreach ($data as $k=>$v){
                    if($v['status'] != GoodsService::GOODS_STATUS_PUT){ //商品未上架  goods_id置空 不让跳转
                        $data[$k]['goods_id'] = '';
                    }
                }
            }else{
                $data[0] = ['banner_img'=>'','goods_id'=>''];
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '店铺不存在');
        }
    }

    /**
     * 获取商户是否开启城市定位
     */
    public function getShopCityLocation(Request $request){
        $member_id = $request->input('member_id');
        if($member_id){
            $is_city_location = ShopUser::where('member_id',$member_id)->value('is_city_location');
            $data['is_city_location'] = $is_city_location;
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '店铺不存在');
        }
    }


    /**
     * 获取店铺分类
     */
    public function getShopCategory(Request $request){
        $member_id = $request->input('member_id');
        if($member_id){
            $data = ShopCategory::where([['member_id',$member_id],['status',ShopCategory::SHOP_CATEGORY_STATUS_ENABLE],['is_delete',ShopCategory::SHOP_CATEGORY_DELETE_NO]])->select('id','cat_name')->orderBy('sort', 'ASC')->get();
            $data = $data ? $data->toArray() : [];
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '店铺不存在');
        }
    }

    /**
     * 获取商户店铺首页商品信息
     */
    public function getShopIndex(Request $request){
        $member_id = $request->input('member_id');
        $city_name = $request->input('city_name');
        $goods_name = $request->input('goods_name');
        $sort_data = $request->input('sort_data',GoodsService::GOODS_SORT_COMPREHENSIVE);
        $page = $request->input('page');
        $pageSize = $request->input('pageSize');
        if($member_id){
            $is_city_location = ShopUser::where('member_id',$member_id)->value('is_city_location');
            if($is_city_location == ShopUser::SHOP_USER_CITY_LOCATION_OPEN){//开启城市定位
                $city_id = Areas::where([['name','like','%'.$city_name.'%'],['level',2]])->value('id');
                if($city_id){
                    $where[] = ['show_ity',$city_id];
                }
            }
            $where[] = ['status',GoodsService::GOODS_STATUS_PUT];
            $where[] = ['is_delete',CommonService::GOODS_IS_DELETE_NOT];
            $where[] = ['member_id',$member_id];
            if(!empty($goods_name)){
                $where[] = ['title','like','%'.$goods_name.'%'];
            }
            $orderby = GoodsService::GOODS_SORT_TYPE_NAME_LIST[$sort_data];
            $upanddown = GoodsService::GOODS_SORT_UP_DOWN_LIST[$sort_data];
            $goods = Goods::where($where)->select('id','title','rent_label','imgurl','min_price','min_lease_term','is_huabei')->orderBy($orderby,$upanddown)->paginate($pageSize);
            $goods = $goods ? $goods->toArray() : [];
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $goods);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '店铺不存在');
        }
    }

    /**
     * 根据分类id获取商户商品
     */
    public function getShopGoods(Request $request){
        $member_id = $request->input('member_id');
        $cate_id = $request->input('cate_id');
        $goods_name = $request->input('goods_name');
        $sort_data = $request->input('sort_data',GoodsService::GOODS_SORT_COMPREHENSIVE);
        $page = $request->input('page');
        $pageSize = $request->input('pageSize');
        if($member_id){
            $where[] = ['g.status',GoodsService::GOODS_STATUS_PUT];
            $where[] = ['g.is_delete',CommonService::GOODS_IS_DELETE_NOT];
            $where[] = ['g.member_id',$member_id];
            if(!empty($goods_name)){
                $where[] = ['g.title','like','%'.$goods_name.'%'];
            }
            if(!empty($cate_id)){
                $where[] = ['s.cat_id',$cate_id];
            }
            $orderby = 'g.'.GoodsService::GOODS_SORT_TYPE_NAME_LIST[$sort_data];
            $upanddown = GoodsService::GOODS_SORT_UP_DOWN_LIST[$sort_data];
            $goods = ShopGoods::from('sp_shop_goods as s')->leftjoin('sp_goods_2 as g','s.goods_id','g.id')->where($where)->select('g.id','g.title','g.rent_label','g.imgurl','g.min_price','g.min_lease_term','g.is_huabei')->orderBy($orderby,$upanddown)->paginate($pageSize);
            $goods = $goods ? $goods->toArray() : [];
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $goods);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '店铺不存在');
        }
    }


    //获取客服电话
    public function getTelePhone(Request $request){
        $member_id = $request->input('member_id');
        if(!empty($member_id)){
            $data['shop_mobile'] = ShopUser::where('member_id',$member_id)->value('mobile');
        }
        $data['platform_mobile'] = config('alipay.Telephone');
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }


    /***
     * 供应商入驻信息获取
     */
    public function getSupplierData(){
        $category = Category::where([['parentid',0],['is_delete',Category::CATEGORY_NOT_DELETE],['enabled',Category::CATEGORY_STATUS_DISPLAY]])->select('id','name')->get();
        $data['category'] = obToArray($category);
        $data['fap_type'] = ShopUser::FAPTYPE_LIST;
        $data['account_type'] = ShopUser::ACCOUNT_TYPE_LIST;
        $data['risk_type'] = ShopUser::SHOP_RISK_LIST;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 获取城市列表
     */
    public function getAreas()
    {
        $data = Areas::GetAreasList();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 供应商申请
     */
    public function supplierApply(Request $request){
        $input = $request->input();
        if (empty($input['shopname']) || empty($input['photo']) || empty($input['shoptype']) || empty($input['people']) || empty($input['contact_tel'])
            || empty($input['faptype']) || empty($input['mechanism']) || empty($input['companyid']) || empty($input['company_area'] || empty($input['company']))
            || empty($input['shopcity']) || empty($input['shopcard']) || empty($input['doorphoto']) || empty($input['seal_url'] || empty($input['faname']))
            || empty($input['idcard']) || empty($input['faidcard']) || empty($input['faidcard_back'])  || empty($input['area_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $shopUser = ShopUser::GetShopUser(array('shopname' => $input['shopname']));
        if (!empty($shopUser)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺名称重复,请重新输入');
        }
        if (!Verify_Phone($input['contact_tel'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效手机号');
        } else {
            if(ShopUser::where('shopname',$input['shopname'])->count() > 0){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '店铺名称已存在');
            }
        }
        if (!isBusinessRegistrationNumber($input['companyid'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效工商注册号');
        }
        if (!check_passportid($input['idcard'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效法人身份证号');
        }
        $time = time();
        $addData = array(
            'shopname' => $input['shopname'], //店铺名称
            'photo' => $input['photo'], //店铺头像
            'shoptype' => $input['shoptype'], //商品类型
            'people' => $input['people'], //联系人
            'contact_tel' => $input['contact_tel'], //联系人手机号
            'mobile' => $input['contact_tel'], //联系人手机号
            'faptype' => $input['faptype'], //发票类型：1普通发票  2增值税发票  1,2普通发票+增值税发票
            'mechanism' => $input['mechanism'], //公司/机构名称
            'companyid' => $input['companyid'], //工商/营业执照注册号
            'company_area' => $input['company_area'], //公司所在区域 省-市-区
            'area_id' => $input['area_id'], //区域城市id
            'company' => $input['company'], //公司地址
            'shopcity' => $input['shopcity'], //当前经营位置 省-市
            'shopcard' => $input['shopcard'], //营业执照图片
            'doorphoto' => $input['doorphoto'], //公司门头照
            'seal_url' => $input['seal_url'], //公章图片
            'faname' => $input['faname'], //法人姓名
            'idcard' => $input['idcard'], //法人身份证号
            'faidcard' => $input['faidcard'], //法人身份证图片
            'faidcard_back' => $input['faidcard_back'], //法人身份证图片(带国徽的一面)
            'category_id' => $input['shoptype'], //申请品类父级id
            'member_id' => $time,
            'shop_balance' => 0,
            'frozen_amount' => 0,
            'last_login_ip' => getRealIp(),
            'addtime' => $time,
        );
        //有三方关联ID进行数据绑定
        if(isset($input['relationId']) && $input['relationId']){
            $addData['relation_id'] = $input['relationId'];
        }
        $res = ShopUser::addData($addData);
        if ($res) {
            //添加操作记录
            ShopUserLog::addLog(0, $time, 2, '供应商入驻', [], [], '申请入驻成为产品供应商');

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '提交成功，请等待平台审核');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '提交失败，请稍后再试或联系平台客服');
        }
    }



}
